package pl.wroc.uni.ii.puzzle.game;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Style;

public class Cell {

	// cell is in proper position if coordinates of these are equals
	private Rect imageRectangle;
	private Rect canvasRectangle;
	
	private boolean isSelected;
	private Bitmap image;
	
	public Cell(int row, int column, int width, int height, Bitmap image) {
		isSelected = false;
		this.image = image;

		imageRectangle = new Rect();
		canvasRectangle = new Rect();
		
		//TODO check this calculations ;P
		int left = width * column;
		int right = left + width;
		int top = height * row;
		int bottom = top + height;

		imageRectangle.set(left, top, right, bottom);
		canvasRectangle.set(left, top, right, bottom);
	}

	public void draw(Canvas canvas) {		
		Paint paint = new Paint();
		// drawImage
		canvas.drawBitmap(image, imageRectangle, canvasRectangle, paint);
		
		// drawSelection
		if (isSelected) {
			paint.setColor(Color.RED);
			paint.setStyle(Style.STROKE);
			paint.setStrokeWidth(2);
			canvas.drawRect(canvasRectangle, paint);
		}
	}
	
	public void select() {
		isSelected = !isSelected;
	}
	
	public boolean hasProperPosition() {
		return (imageRectangle.left == canvasRectangle.left
				&& imageRectangle.top == canvasRectangle.top);
	}
	
	public boolean isSelected() {
		return isSelected;
	}
	
	public void setPosition(Rect rect) {
		imageRectangle = rect;
	}
	
	public Rect getPosition() {
		return imageRectangle;
	}
}
